library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Warning: package 'sf' was built under R version 4.2.3
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.2.3
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(osmdata)
## Warning: package 'osmdata' was built under R version 4.2.3
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(mapview)
sf_use_s2(FALSE)
## Spherical geometry (s2) switched off

make_bbox() st_bbox() getbb()

bbox_montevideo <- getbb("Montevideo, Uruguay")
bbox_montevideo
##         min       max
## x -56.43140 -56.02250
## y -34.93806 -34.70185
mapa_montevideo <- get_stamenmap(bbox=bbox_montevideo,zoom=12)
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
ggmap(mapa_montevideo)

polygon_montevideo <- getbb("Montevideo, Uruguay",format_out = "sf_polygon")
polygon_montevideo <- polygon_montevideo %>% mutate(ID=row.names(polygon_montevideo))
ggmap(mapa_montevideo)+
  geom_sf(data=polygon_montevideo, inherit.aes = FALSE)+
  facet_wrap(~ID)
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

polygon_montevideo <- polygon_montevideo %>%
  filter(ID==1)
ggmap(mapa_montevideo)+
  geom_sf(data=polygon_montevideo, inherit.aes = FALSE, fill=NA, color= "blue")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

descarga de parques

paso 1:seleccionar box opq()

parques_montevideo <- opq(bbox_montevideo)

paso2: elegir que descargar: key=leisure, value=park add_osm_featurre()

parques_montevideo <- add_osm_feature(parques_montevideo,
                                      key="leisure",
                                      value="park")

paso3: Descargar datos osmdata_sf()

parques_montevideo <- osmdata_sf(parques_montevideo)
parques_montevideo
## Object of class 'osmdata' with:
##                  $bbox : -34.938056,-56.4313997,-34.7018526,-56.0225006
##         $overpass_call : The call submitted to the overpass API
##                  $meta : metadata including timestamp and version numbers
##            $osm_points : 'sf' Simple Features Collection with 3724 points
##             $osm_lines : 'sf' Simple Features Collection with 36 linestrings
##          $osm_polygons : 'sf' Simple Features Collection with 116 polygons
##        $osm_multilines : NULL
##     $osm_multipolygons : 'sf' Simple Features Collection with 10 multipolygons

paso4: seleccionar los poligonos

parques_polygons <- parques_montevideo$osm_polygons
parques_multipolygons <- parques_montevideo$osm_multipolygons
ggmap(mapa_montevideo)+
  geom_sf(data=polygon_montevideo, inherit.aes = FALSE, fill=NA, color="blue")+
  geom_sf(data=parques_polygons, inherit.aes = FALSE)+
  geom_sf(data=parques_multipolygons, inherit.aes = FALSE)
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

rbind()

parques_polygons <- parques_polygons %>%
  select(osm_id, name)
parques_multipolygons <- parques_multipolygons %>%
  select(osm_id, name)
parques_montevideo <- rbind(parques_polygons, parques_multipolygons)

st_intersection()

ggmap(mapa_montevideo)+
  geom_sf(data=polygon_montevideo, inherit.aes = FALSE, fill=NA, color="blue")+
  geom_sf(data=parques_montevideo, inherit.aes = FALSE, fill="green")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

parques_montevideo <- st_intersection(parques_montevideo, polygon_montevideo)
## although coordinates are longitude/latitude, st_intersection assumes that they
## are planar
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
ggmap(mapa_montevideo)+
  geom_sf(data=polygon_montevideo, inherit.aes = FALSE, fill=NA, color="blue")+
  geom_sf(data=parques_montevideo, inherit.aes = FALSE, fill="green")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

available_features()
##   [1] "4wd_only"                    "abandoned"                  
##   [3] "abutters"                    "access"                     
##   [5] "addr"                        "addr:city"                  
##   [7] "addr:conscriptionnumber"     "addr:country"               
##   [9] "addr:county"                 "addr:district"              
##  [11] "addr:flats"                  "addr:full"                  
##  [13] "addr:hamlet"                 "addr:housename"             
##  [15] "addr:housenumber"            "addr:inclusion"             
##  [17] "addr:interpolation"          "addr:place"                 
##  [19] "addr:postbox"                "addr:postcode"              
##  [21] "addr:province"               "addr:state"                 
##  [23] "addr:street"                 "addr:subdistrict"           
##  [25] "addr:suburb"                 "addr:unit"                  
##  [27] "admin_level"                 "aeroway"                    
##  [29] "agricultural"                "alt_name"                   
##  [31] "amenity"                     "area"                       
##  [33] "atv"                         "backward"                   
##  [35] "barrier"                     "basin"                      
##  [37] "bdouble"                     "bicycle"                    
##  [39] "bicycle_road"                "biergarten"                 
##  [41] "boat"                        "border_type"                
##  [43] "boundary"                    "brand"                      
##  [45] "bridge"                      "building"                   
##  [47] "building:colour"             "building:fireproof"         
##  [49] "building:flats"              "building:levels"            
##  [51] "building:material"           "building:min_level"         
##  [53] "building:part"               "building:soft_storey"       
##  [55] "bus_bay"                     "busway"                     
##  [57] "capacity"                    "castle_type"                
##  [59] "change"                      "charge"                     
##  [61] "clothes"                     "construction"               
##  [63] "construction#Railways"       "construction_date"          
##  [65] "covered"                     "craft"                      
##  [67] "crossing"                    "crossing:island"            
##  [69] "cuisine"                     "cutting"                    
##  [71] "cycleway"                    "denomination"               
##  [73] "destination"                 "diet:*"                     
##  [75] "direction"                   "dispensing"                 
##  [77] "disused"                     "drinking_water"             
##  [79] "drive_in"                    "drive_through"              
##  [81] "ele"                         "electric_bicycle"           
##  [83] "electrified"                 "embankment"                 
##  [85] "embedded_rails"              "emergency"                  
##  [87] "end_date"                    "entrance"                   
##  [89] "est_width"                   "fee"                        
##  [91] "female"                      "fire_object:type"           
##  [93] "fire_operator"               "fire_rank"                  
##  [95] "foot"                        "footway"                    
##  [97] "ford"                        "forestry"                   
##  [99] "forward"                     "frequency"                  
## [101] "fuel"                        "gauge"                      
## [103] "golf_cart"                   "goods"                      
## [105] "hazard"                      "hazmat"                     
## [107] "healthcare"                  "healthcare:counselling"     
## [109] "healthcare:speciality"       "height"                     
## [111] "hgv"                         "highway"                    
## [113] "historic"                    "horse"                      
## [115] "hot_water"                   "ice_road"                   
## [117] "incline"                     "industrial"                 
## [119] "inline_skates"               "inscription"                
## [121] "int_name"                    "internet_access"            
## [123] "junction"                    "kerb"                       
## [125] "landuse"                     "lanes"                      
## [127] "lanes:bus"                   "lanes:psv"                  
## [129] "layer"                       "leaf_cycle"                 
## [131] "leaf_type"                   "leisure"                    
## [133] "lhv"                         "lit"                        
## [135] "loc_name"                    "location"                   
## [137] "male"                        "man_made"                   
## [139] "max_age"                     "max_level"                  
## [141] "maxaxleload"                 "maxheight"                  
## [143] "maxlength"                   "maxspeed"                   
## [145] "maxstay"                     "maxweight"                  
## [147] "maxwidth"                    "military"                   
## [149] "min_age"                     "min_level"                  
## [151] "minspeed"                    "mofa"                       
## [153] "moped"                       "motor_vehicle"              
## [155] "motorboat"                   "motorcar"                   
## [157] "motorcycle"                  "motorroad"                  
## [159] "mountain_pass"               "mtb:description"            
## [161] "mtb:scale"                   "name"                       
## [163] "name:left"                   "name:right"                 
## [165] "name_1"                      "name_2"                     
## [167] "narrow"                      "nat_name"                   
## [169] "natural"                     "nickname"                   
## [171] "noexit"                      "non_existent_levels"        
## [173] "nudism"                      "office"                     
## [175] "official_name"               "old_name"                   
## [177] "oneway"                      "oneway:bicycle"             
## [179] "opening_hours"               "opening_hours:drive_through"
## [181] "operator"                    "operator:type"              
## [183] "orientation"                 "oven"                       
## [185] "overtaking"                  "parking"                    
## [187] "parking:condition"           "parking:lane"               
## [189] "passing_places"              "place"                      
## [191] "power"                       "power_supply"               
## [193] "priority"                    "priority_road"              
## [195] "produce"                     "proposed"                   
## [197] "protected_area"              "psv"                        
## [199] "public_transport"            "railway"                    
## [201] "railway:preserved"           "railway:track_ref"          
## [203] "recycling_type"              "ref"                        
## [205] "reg_name"                    "religion"                   
## [207] "rental"                      "residential"                
## [209] "roadtrain"                   "route"                      
## [211] "sac_scale"                   "sauna"                      
## [213] "service"                     "service_times"              
## [215] "shelter_type"                "shop"                       
## [217] "short_name"                  "shower"                     
## [219] "sidewalk"                    "site"                       
## [221] "ski"                         "smoothness"                 
## [223] "social_facility"             "sorting_name"               
## [225] "speed_pedelec"               "start_date"                 
## [227] "step_count"                  "substation"                 
## [229] "surface"                     "tactile_paving"             
## [231] "tank"                        "tidal"                      
## [233] "toilets"                     "toilets:wheelchair"         
## [235] "toll"                        "topless"                    
## [237] "tourism"                     "tracks"                     
## [239] "tracktype"                   "traffic_calming"            
## [241] "traffic_sign"                "trail_visibility"           
## [243] "trailblazed"                 "trailblazed:visibility"     
## [245] "tunnel"                      "turn"                       
## [247] "type"                        "unisex"                     
## [249] "usage"                       "vehicle"                    
## [251] "vending"                     "voltage"                    
## [253] "water"                       "wheelchair"                 
## [255] "wholesale"                   "width"                      
## [257] "winter_road"                 "wood"
available_tags("shop")
## # A tibble: 172 × 2
##    Key   Value                                                              
##    <chr> <chr>                                                              
##  1 shop  [[ Too many Data Items entities accessed. |  musical_instrument  ]]
##  2 shop  agrarian                                                           
##  3 shop  alcohol                                                            
##  4 shop  anime                                                              
##  5 shop  antiques                                                           
##  6 shop  appliance                                                          
##  7 shop  art                                                                
##  8 shop  atv                                                                
##  9 shop  baby_goods                                                         
## 10 shop  bag                                                                
## # ℹ 162 more rows
available_tags("leisure")
## # A tibble: 34 × 2
##    Key     Value              
##    <chr>   <chr>              
##  1 leisure adult_gaming_centre
##  2 leisure amusement_arcade   
##  3 leisure bandstand          
##  4 leisure beach_resort       
##  5 leisure bird_hide          
##  6 leisure common             
##  7 leisure dance              
##  8 leisure disc_golf_course   
##  9 leisure dog_park           
## 10 leisure escape_game        
## # ℹ 24 more rows
parques_montevideo <- parques_montevideo %>%
  mutate(area_km2=st_area(parques_montevideo))